﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCL.Utility;
using System.Data.SqlClient;

namespace Server.Service
{
    /// <summary>
    /// 회원가입 요청에 대한 서비스를 제공합니다.
    /// </summary>
    public class Join : IService
    {
        /// <summary>
        /// 요청받은 E-Mail 주소를 가져옵니다.
        /// </summary>
        public string Email { get; private set; }

        /// <summary>
        /// 요청받은 Password를 가져옵니다.
        /// </summary>
        public string Password { get; private set; }

        /// <summary>
        /// 요청받은 이름을 가져옵니다.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// 새로운 서비스를 생성합니다.
        /// </summary>
        public Join(char[] email, char[] password, char[] name)
        {
            Email = StringUtility.ConvertToStr(email);
            Password = StringUtility.ConvertToStr(password);
            Name = StringUtility.ConvertToStr(name);
        }

        /// <summary>
        /// 서비스를 시작합니다.
        /// </summary>
        /// <returns>전송할 byte배열</returns>
        public byte[] Execute()
        {
            byte[] returnBytes = null;

            using (SqlConnection conn = new SqlConnection(Properties.Resources.ConnectionString))
            {
                try
                {
                    conn.Open();

                    SqlCommand sqlCmd;       

                    sqlCmd = new SqlCommand(@"SELECT * FROM [User] WHERE email = @email", conn);
                    sqlCmd.Parameters.AddWithValue("@email", Email);
                    
                    SqlDataReader reader = sqlCmd.ExecuteReader();

                    if (reader.HasRows)
                        throw new Exception("이미 존재하는 이메일입니다.");

                    reader.Close();

                    sqlCmd = new SqlCommand(@"INSERT INTO [User](email, name, password, joinedDateTime) VALUES (@email, @name, @password, @datetime)", conn);
                    sqlCmd.Parameters.AddWithValue("@email", Email);
                    sqlCmd.Parameters.AddWithValue("@name", Name);
                    sqlCmd.Parameters.AddWithValue("@password", Password);
                    sqlCmd.Parameters.AddWithValue("@datetime", DateTime.Now.ToShortDateString());

                    int execution = sqlCmd.ExecuteNonQuery();

                    if (execution <= 0)
                        throw new System.Exception("회원가입이 정상처리 되지 않았습니다.");

                    CCL.SocketMessage.Response.Join response = new CCL.SocketMessage.Response.Join();
                    CCL.SocketMessage.Message respMsg = CCL.SocketMessage.Message.MessageResponseBuilder(response);

                    returnBytes = respMsg.GetByte();
                }
                catch (System.Exception ex)
                {
                    CCL.SocketMessage.Response.Join response = new CCL.SocketMessage.Response.Join(ex.Message);
                    CCL.SocketMessage.Message respMsg = CCL.SocketMessage.Message.MessageResponseBuilder(response);

                    returnBytes = respMsg.GetByte();
                }
            }

            return returnBytes;
        }
    }
}
